package com.bristua.mall.user.biz;

import com.bristua.mall.common.enums.CommonCode;
import com.bristua.mall.common.exception.BizException;
import com.bristua.mall.common.utils.CacheUtil;
import com.bristua.mall.common.utils.DateUtil;
import com.bristua.mall.common.utils.SequenceUtil;
import com.bristua.mall.user.dao.TUserAccountMapper;
import com.bristua.mall.user.dao.TUserBankMapper;
import com.bristua.mall.user.dao.TUserCashTakeMapper;
import com.bristua.mall.user.model.TUser;
import com.bristua.mall.user.model.TUserAccount;
import com.bristua.mall.user.model.TUserBank;
import com.bristua.mall.user.model.TUserCashTake;
import com.bristua.mall.user.model.condition.TUserAccountExample;
import com.bristua.mall.user.model.condition.applyTakeCash.ApplyTakeCash;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2019/3/25.
 */
@Service
@Transactional
public class ApplyTakeCashBiz {

    @Resource
    private TUserAccountMapper tUserAccountMapper;

    @Resource
    private TUserCashTakeMapper tUserCashTakeMapper;

    @Resource
    private TUserBankMapper tUserBankMapper;

    @Autowired
    private CacheUtil cacheUtil;

    public void applyTakeCash(ApplyTakeCash applyTakeCash){
        TUser userSession = (TUser) cacheUtil.getUserSession();
        String userId = userSession.getUserId();
        TUserAccountExample tUserAccountExample = new TUserAccountExample();
        tUserAccountExample.createCriteria().andUserIdEqualTo(userId);
        List<TUserAccount> tUserAccounts = tUserAccountMapper.selectByExample(tUserAccountExample);
        TUserBank tUserBank = tUserBankMapper.selectByPrimaryKey(applyTakeCash.getBankId());

        if(tUserBank==null){
            throw new BizException(CommonCode.UILLEGAL_OPERATION_EXCEPTION.getCode(),CommonCode.UILLEGAL_OPERATION_EXCEPTION.getMsg());
        }

        if(tUserAccounts!=null && tUserAccounts.size()>0){
            TUserAccount tUserAccount = tUserAccounts.get(0);
            BigDecimal userIngegralBig = new BigDecimal(StringUtils.isEmpty(tUserAccount.getUserIntegral())?"0":tUserAccount.getUserIntegral());
            BigDecimal cashBig = new BigDecimal(applyTakeCash.getCash());
            if(userIngegralBig.compareTo(cashBig)==-1){
                throw new BizException(CommonCode.UILLEGAL_OPERATION_EXCEPTION.getCode(),CommonCode.UILLEGAL_OPERATION_EXCEPTION.getMsg());
            }
            BigDecimal bigDecimal =userIngegralBig.subtract(cashBig);
            tUserAccount.setUserIntegral(bigDecimal.toString());
//            tUserAccount.setUserIntegral(Integer.valueOf(applyTakeCash.getCash()));
            tUserAccountMapper.updateByPrimaryKeySelective(tUserAccount);
        }

        TUserCashTake tUserCashTake = new TUserCashTake();
        tUserCashTake.setCreateTime(DateUtil.getDateTime3());
        tUserCashTake.setStatus("1");
        tUserCashTake.setTakeCash(applyTakeCash.getCash());
        tUserCashTake.setUserId(userId);
        tUserCashTake.setUserTakeCashId(SequenceUtil.nextId());
        tUserCashTake.setBankId(applyTakeCash.getBankId());
        tUserCashTakeMapper.insert(tUserCashTake);
    }
}
